package com.ddyx.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author cj
 */
public class DateUtil {

	public static SimpleDateFormat format_ymd_hms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public static SimpleDateFormat format_ymd = new SimpleDateFormat("yyyy-MM-dd");
	
	public static String getNow(){
		return format_ymd_hms.format(new Date());
	}
	
	public static String getToday_Ymd(){
		return format_ymd.format(new Date());
	}
	
	public static String getFirstDayOfThisWeek_Ymd(){
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int d = 0;
		if(cal.get(Calendar.DAY_OF_WEEK)==1){
			d = -6;
		}else{
			d = 2-cal.get(Calendar.DAY_OF_WEEK);
		}
		cal.add(Calendar.DAY_OF_WEEK, d);
		return format_ymd.format(cal.getTime());
	}
	
	public static String getLastDayOfThisWeek_Ymd(){
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int d = 0;
		if(cal.get(Calendar.DAY_OF_WEEK)==1){
			d = -6;
		}else{
			d = 2-cal.get(Calendar.DAY_OF_WEEK);
		}
		cal.add(Calendar.DAY_OF_WEEK, d);
		cal.add(Calendar.DAY_OF_WEEK, 6);
		return format_ymd.format(cal.getTime());
	}
	
	 public static int daysBetween(String smdate,String bdate) throws ParseException{  
	        Calendar cal = Calendar.getInstance();    
	        cal.setTime(format_ymd.parse(smdate));    
	        long time1 = cal.getTimeInMillis();                 
	        cal.setTime(format_ymd.parse(bdate));    
	        long time2 = cal.getTimeInMillis();         
	        long between_days=(time2-time1)/(1000*3600*24);  
	       return Integer.parseInt(String.valueOf(between_days));     
	    }  
	 
	 public static String getSpecifiedDayBefore(String specifiedDay)throws ParseException{ 
		Calendar c = Calendar.getInstance(); 
		c.setTime(format_ymd.parse(specifiedDay)); 
		int day=c.get(Calendar.DATE); 
		c.set(Calendar.DATE,day-1);
		String dayBefore=format_ymd.format(c.getTime()); 
		return dayBefore; 
		} 
	 
	//获取前后日期 i为正数 向后推迟i天，负数时向前提前i天
	 public static String getdate(String day,int i) throws ParseException {
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(format_ymd.parse(day)); 
		 cal.add(Calendar.DATE, i);
		 String date=format_ymd.format(cal.getTime()); 
		 return date;
	 }
	
}
